import {combineReducers} from 'redux-immutable';
import Immutable from 'immutable';
import * as actions from './actions';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION} from './constants';
const EMPTY_ARRAY = [];
export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(
                Immutable.fromJS({
                    isFetching: true,
                    message: ''
                })
            );
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(
                Immutable.fromJS({
                    isFetching: false,
                    message: '',
                    data: action.data
                })
            );
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(
                Immutable.fromJS({
                    isFetching: false,
                    message: action.message
                })
            );
        default:
            return state;
    }
};

const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                total: 0
            });
        case actions.GET_LIST_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                message: '',
                data: action.data.content
            });
        case actions.GET_LIST_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                message: action.message || '',
                data: EMPTY_ARRAY
            });
        default:
            return state;
    }
};

const queryCondition = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.set(action.name, action.value);
        case actions.RESET_QUERY_PANEL: {
            const {pageSize, sortField, isDesc} = state.toJS();
            return Immutable.fromJS({
                ...DEFAULT_QUERY_OPTION,
                pageSize,
                sortField,
                isDesc
            });
        }
        case actions.GET_LIST_SUCCESS:
            return state.withMutations(map =>
                map
                    .set('pageSize', action.condition.pageSize)
                    .set('sortField', action.condition.sortField)
                    .set('isDesc', action.condition.isDesc)
                    .set('faultModeCategoryId', action.condition.faultModeCategoryId)
            );

        default:
            return state;
    }
};

const queryBySort = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};

const categories = (state, action) => {
    switch(action.type) {
        case actions.GET_CATEGORY_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                data: []
            });
        case actions.GET_CATEGORY_SUCCESS: {
            if(!action.path.length)
                return state.merge({
                    isFetching: false,
                    message: '',
                    data: action.data
                });
            return state.setIn(['data', ...action.path, 'items'], Immutable.fromJS(action.data));
        }
        case actions.GET_CATEGORY_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message || '',
                data: EMPTY_ARRAY
            });
        case actions.ADD_CATEGORY: {
            if(!action.path.length)
                return state.updateIn(['data'], list => list.push(Immutable.fromJS(action.data)));
            return state.withMutations(map => map
                .updateIn(['data', ...action.path, 'items'], list => list.push(Immutable.fromJS(action.data)))
                .setIn(['data', ...action.path, 'isLeaf'], false)
            );
        }
        case actions.UPDATE_CATEGORY: {
            return state.updateIn(['data', ...action.path], obj => obj.merge(action.data));
        }
        case actions.ABANDON_CATEGORY: {
            return state.deleteIn(['data', ...action.path]);
        }
        default:
            return state;
    }
};


const domainData = combineReducers({
    list,
    permission,
    categories,
});
const appState = combineReducers({
    queryCondition,
    queryBySort,
});
export default combineReducers({
    domainData,
    appState,
});
